//index.js
//获取应用实例
const app = getApp();
var QQMapWX = require('../../utils/qqmap-wx-jssdk.min.js');
const QQkey = "26VBZ-WSXW4-OMZU5-XL3BY-NDPH6-TUFV7";

Page({
  data: {
    latitude: 24.366977,
    longitude: 118.046401,
    scale: 17,
    markers: [
      {
        id: 1,
        iconPath: "../../libs/icons/location.png",
        width: 70,
        height: 70,
        latitude: 24.366977,
        longitude: 118.046401,
      }
    ]
  },

  onLoad: function () {
    let that = this;
    // // 实例化API核心类
    var qqmapsdk = new QQMapWX({
      key: QQkey
    });
    wx.getLocation({
      success: function (res) {
        console.log(res);
        // that.setData({
        //   longitude:res.longitude,
        //   latitude:res.latitude
        // })
      },
    });

    //设置地图控件位置大小等
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          controls: [
            {
              id: 1,
              iconPath: '../../libs/icons/P.png',
              position: {
                left: 20,
                top: res.windowHeight - 180,
                width: 60,
                height: 60
              },
              clickable: true
            },
            {
              id: 2,
              iconPath: '../../libs/icons/T.png',
              position: {
                left: 20,
                top: res.windowHeight - 110,
                width: 60,
                height: 60
              },
              clickable: true
            },
            {
              id: 3,
              iconPath: '../../libs/icons/tree.png',
              position: {
                left: res.windowWidth - 100,
                top: res.windowHeight - 145,
                width: 100,
                height: 70
              },
              clickable: true
            },
            {
              id: 4,
              iconPath: '../../libs/icons/scal.png',
              position: {
                top: res.windowHeight - 45,
                width: res.windowWidth,
                height:45
              },
              clickable: true
            }
          ]
        })
      },
    });
    //获取后台数据(显示箱子位置)
    // wx.request({
    //   url: '',
    //   data:{},
    //   method:'POST',
    //   success:(res)=>{
    //     that.setData({
    //       markers:res.data
    //     });
    //   },
    //   fail:(res)=>{

    //   }
    // })
  },
  //地图控件点击事件
  bindcontroltap: e => {
    switch (e.controlId) {
      case 1:
        //获取后台数据，拿到可以捐纸的地方
        wx.showModal({
          title: 'Test',
          content: '点击控件P,当前没接入后台数据',
          showCancel:false,
        });
        break;
      case 2:
        //获取后台数据,拿到可以捐纸的地方
        console.log("点击T");
        wx.showModal({
          title: 'Test',
          content: '点击控件T,当前没接入后台数据',
          showCancel: false,
        });
        break;
      case 3:
        //检查是否有用户数据，有则跳转，无则登录
        console.log("点击大树");
        wx.getStorage({
          key: 'userID',
          success: function (res) {
            console.log("检查到用户数据，可以扫码使用");
            wx.navigateTo({
              url: '../tree/tree',
            })
          },
          fail: (res) => {
            console.log("未检查到用户数据，前往登录");
            wx.navigateTo({
              url: '../login/login',
            });
          }
        });
        break;
      case 4:
        wx.getStorage({
          key: 'userID',
          success: function (res) {
            console.log("检查到用户数据，可以扫码使用");
            wx.scanCode({
              onlyFromCamera: true,//仅允许用户从相机扫码
              success: (res) => {
                wx.showModal({
                  title: '扫描结果',
                  content: `扫描到的内容是 ${res.result}`,
                  showCancel: false,
                })
              }
            })
          },
          fail: (res) => {
            console.log("未检查到用户数据，前往登录");
            wx.navigateTo({
              url: '../login/login',
            })
          }
        });
        break;
      default:
        break;
    }
  },
  //设置转发
  onShareAppMessage:res=>{
    return{
      title:'纸要有你',
      path:'/page/index/index',
    }
  }
})
